<?php

namespace app\stores\controller\miniapp\goods;

use app\BaseController;

use app\beautiful\model\goods\GoodsGroupDetailModel;
use app\beautiful\model\goods\GoodsGroupModel;
use app\beautiful\model\goods\GoodsOtherSkuDetailModel;
use app\beautiful\model\logs\GoodsViewsModel;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\member\MemberModel;
use app\common\model\ShopModel;
use app\stores\model\goods\GoodsCollectModel;
use app\stores\model\promotion\ActivityModel;
use app\stores\model\promotion\TeamActivityModel;


class SetMeal extends BaseController{
    /*套餐详情*/
    public function getSetMealDetail(){
        $lng =  $this->request->param('lng/f',0);
        $lat =  $this->request->param('lat/f',0);
        $goods_id = $this->request->param('goods_id/d',0);
        $pmid =  $this->request->param('pmid/d',0);
        $taId =  $this->request->param('taId/d',0);
        //保存用户门店中等级信息
        if($this->request->shopId && $this->request->memberId){
            saveMemberLevel($this->request->shopId,$this->request->memberId);
        }

        if(null === $goods = GoodsModel::with(['unit'])->where(['goods_id'=>$goods_id,'type'=>3])->json(['discount_detail','use_limit_detail'],true)->find()) return $this->error('套餐不存在');

        $goods = $goods->toArray();

        if($goods['shop_id'] != $this->request->shopId) return $this->error('套餐不存在');

        if($goods['type'] != 3) return $this->error('套餐不存在');

        if($goods['is_delete'] == 1) return $this->error('套餐不存在');

        if($goods['is_open_yu'] != 0) return $this->error('该套餐为预售套餐');

        if($goods['status'] != 1) return $this->error('套餐已下架');

        //通用门店
        $general_shop_ids = $goods['general_shop']?explode(',',$goods['general_shop']):[];

        $general_shop_ids[] =  $this->request->shopId;

        $general_shop_list = ShopModel::with(['detail','kefu'])->whereIn('shop_id',$general_shop_ids)->field('shop_id,name,customer_service_id')->select()->toArray();

        foreach ($general_shop_list as $k=>$v){
            $general_shop_list[$k] = $this->generalShop($v);
            $general_shop_list[$k]['distance'] = get_distance($lat,$lng,$v['detail']['t_lat'],$v['detail']['t_lng']);
        }
        //套餐分组
        $goods_group = GoodsGroupModel::with(['groupDetail'=>function($query){
                $query->where(['is_delete'=>0]);
            }])->where([['goods_id','=',$goods_id],['is_delete','=',0]])
                ->field('goods_group_id,group_name,type,num,choose_num,is_choose_same')
                ->select()->toArray() ?? [];
        $total_price = 0;
        foreach ($goods_group as $k=>$v){

            $return = $this->groupDetail($v['groupDetail'],$total_price);

            $total_price += $return['total_price'];

            $goods_group[$k]['groupDetail'] = $return['groupDetail'];

        }
        //折扣
        $member=$this->request->member;
        if(!$member && $this->request->memberId){
            $member = MemberModel::find($this->request->memberId);
        }
        $return = getMemberPrice($member,$this->request->shopId,$goods,$goods['total_value'],$goods['type'],$pmid,$taId);
        $goods['price'] = $return['price'];

        //库存
        if($taId>0){
            $team_activity = TeamActivityModel::where(['team_activity_id'=>$taId,'status'=>1,'is_delete'=>0])->find();
            if($team_activity) {
                $activity = ActivityModel::where(['activity_id' => $team_activity->activity_id, 'status' => 1, 'is_delete' => 0])->find();
                if($activity){
                    $goods['quota'] = $activity['quota'];
                    $goods['name'] = $activity['title'];
                }
            }
        }
        $shop = ShopModel::with(['detail'])->where(['shop_id'=>$this->request->shopId])->find();

        $goods['group'] = $goods_group;

        //单位
        $goods['unit'] = $goods['unit']?$goods['unit']['group_detail_name']:null;
        
        $detail=[

            'goods'=>$goods,

            'shop_name'=>$shop->name,

            'general_shop'=>[

                'count'=>count($general_shop_list),

                'list'=>$general_shop_list
            ],

            'distance'=>get_distance($lat,$lng,$shop->detail->t_lat,$shop->detail->t_lng),
        ];

        //记录产品的访问记录

        $GoodsViewsModel = new GoodsViewsModel();

        $GoodsViewsModel->save([

            'shop_id'   => $this->request->shopId,

            'goods_id'  => $goods_id

        ]);

        return $this->success($detail);



    }
    protected function generalShop($generalShop){
        $generalShop['tel'] = $generalShop['kefu']?$generalShop['kefu']['mobile']:null;
        $generalShop['address'] = $generalShop['detail']['address'];
        $generalShop['logo'] = $generalShop['detail']['logo'];
        $generalShop['lat'] = $generalShop['detail']['t_lat'];
        $generalShop['lng'] = $generalShop['detail']['t_lng'];

        unset($generalShop['imglst']);
        unset($generalShop['detail']);
        unset($generalShop['kefu']);

        return $generalShop;
    }
    protected function groupDetail($groupDetail,$total_price){
        foreach ($groupDetail as $k=>$v){
            if($v['goods_id']){
                $goods = GoodsModel::where(['shop_id'=>$this->request->shopId,'goods_id'=>$v['goods_id'],'is_delete'=>0])->find();
                $groupDetail[$k]['goods_name'] = $goods->name;
                $groupDetail[$k]['original_price'] = $goods->original_price;

                $groupDetail[$k]['unit'] = GoodsOtherSkuDetailModel::where(['shop_id'=>$this->request->shopId,'other_sku_detail_id'=>$goods['unit_id'],'is_delete'=>0])->value('group_detail_name');

                $total_price += $goods->original_price;
            }else{
                $groupDetail[$k]['goods_name'] = $v['goods_name'];
                $groupDetail[$k]['original_price'] = $v['original_price'];
                $groupDetail[$k]['unit'] = $v['unit_name'];

            }
            $total_price += $groupDetail[$k]['original_price'];
            $groupDetail[$k]['total_goods_price'] = $v['original_price']*$v['num'];
        }
        $return['total_price'] = $total_price;
        $return['groupDetail'] = $groupDetail;
        return $return;
    }
}

